import pybonjour
import threading, os, sys, signal, select
import logging

class listener(threading.Thread):
    def __init__(self, protocol, callback, fail_cb):
        self.callback = callback
        self.timeout = 5
        self.resolved = []
        self.protocol = protocol
        self.failed = fail_cb
    
    def mini_cb(self, *args):
        logging.info(str(args))
        
    def run(self):
        browse_sdRef = pybonjour.DNSServiceBrowse(regtype = self.protocol,
                                          callBack = self.mini_cb)
        try:
            while True:
                rdy = select.select([browse_sdRef], [], [])
                if browse_sdRef in rdy:
                    pybonjour.DNSServiceProcessResult(browse_sdRef)
        finally:
            browse_sdRef.close()
            wx.CallAfter(self.failed)
